package com.yueqian.controller;

import com.yueqian.domain.Permission;
import com.yueqian.domain.Role;
import com.yueqian.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping("findAll.do")
//    @RolesAllowed("ADMIN")
    public ModelAndView findAll() throws Exception {
        List<Role> roleList = roleService.findAll();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("role-list");
        mav.addObject("roleList", roleList);
        return mav;
    }

    @RequestMapping("save.do")
    public String save(Role role) throws Exception {
        roleService.save(role);
        return "redirect:findAll.do";
    }

    @RequestMapping("findRoleAndNoPermission.do")
    public ModelAndView findRoleAndNoPermission(String id) throws Exception {
        List<Permission> permissionList = roleService.findRoleAndNoPermission(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("permissionList", permissionList);
        mav.addObject("roleId", id);
        mav.setViewName("role-permission-add");
        return mav;
    }

    @RequestMapping("addPermissionToRole.do")
    public String addPermissionToRole(String roleId, String[] ids) throws Exception {
        roleService.addPermissionToRole(roleId, ids);
        return "redirect:findAll.do";
    }
}
